package algorithm_study.leecode;

import java.util.HashMap;
import java.util.Map;

public class LongestConsecutiveSequence {
    public static void main(String[] args) {
        LongestConsecutiveSequence sequence = new LongestConsecutiveSequence();
        int[]nums=new int[]{0,-1};
        sequence.longestConsecutive(nums);
    }
    private int len=1;
    public int longestConsecutive(int[] nums) {
        int n=nums.length;
        if(n<=1)return n;
        Map<Integer,Boolean> map=new HashMap(n);
        for(int i=0;i<n;i++){
            map.put(nums[i],false);
        }
        for(Integer key:map.keySet()){
            if(!map.get(key)){
                int newLen1= increment(map,key,0);
                int newLen2= increment(map,key,0);
                len=Math.max(len,newLen1+newLen2);
            }
        }
        return len;
    }
    private int increment(Map<Integer,Boolean> map,Integer key,int len){
        if(!map.getOrDefault(key,true)){
            map.replace(key,true);
            return increment(map,++key,len+1);
        }
        return len;
    }
    private int decrement(Map<Integer,Boolean> map,Integer key,int len){
        if(!map.getOrDefault(key,true)){
            map.replace(key,true);
            return decrement(map,--key,len+1);
        }
        return len;
    }
}
